import 'dart:io';
import 'dart:ui';
import 'package:safemap/safemap.dart';
import 'package:wechat_assets_picker/wechat_assets_picker.dart';
import 'package:wechat_assets_picker_plus/src/asset_util.dart';

// 资源模型
class AssetModel {
  // 获取缩略图路径
  static getThumUrl(String ossUrl, String fileName) {
    String thumb =
        '$ossUrl$fileName?x-oss-process=image/resize,w_300/quality,q_90';
    return thumb;
  }

  // 获取图路径
  static getUrl(String ossUrl, String fileName) {
    return '$ossUrl$fileName';
  }

  AssetModel.fromMap(Map<dynamic, dynamic>? map) {
    SafeMap safeMap = SafeMap(map);
    id = safeMap["id"].string ?? "";
    size = Size(
      safeMap["w"].doubleValue ?? 0.0,
      safeMap["h"].doubleValue ?? 0.0,
    );
    src = safeMap["src"].string ?? "";
    thumb = safeMap["thumb"].string ?? "";
    filePath = safeMap["file_path"].string ?? "";
    thumbFilePath = safeMap["thumb_file_path"].string ?? "";
  }

  // 模型转json
  Map<String, dynamic> toMap() {
    Map<String, dynamic> map = Map.from({});
    map['id'] = id;
    map['src'] = src;
    map['thumb'] = thumb;
    map['w'] = size?.width;
    map['h'] = size?.height;
    map['file_path'] = filePath;
    map['thumb_file_path'] = thumbFilePath;
    return map;
  }

  // “➕” 模型
  static AssetModel get add => AssetModel.fromMap({}..addAll({"id": "0"}));

  // 是否是 “➕” 模型
  bool get isAdd => id == "0";

  // 是否支持删除
  bool isSupportDelete = true;

  // id
  String id = "";

  // 宽、高
  Size? size;

  // 地址
  String src = "";

  // 缩略图
  String thumb = "";

  // 文件大小
  int fileSize = 0;

  // 类型
  AssetType _assetType = AssetType.other;
  AssetType get type {
    if (src.isNotEmpty) {
      _assetType = AssetTypeUtils.parse(src);
    } else {
      _assetType = AssetTypeUtils.parse(file.path);
    }
    return _assetType;
  }

  set type(AssetType type) {
    _assetType = type;
  }

  // 本地 - 资源文件
  String filePath = '';
  File get file => File(filePath);

  // 本地 - 缩略图
  String thumbFilePath = '';
  File get thumbFile => File(thumbFilePath);
}
